using I2.Loc;
using UnityEngine;

public class InGameTutorial : EventCommander
{
	public bool inited;

	[SerializeField]
	private string TutorialRootFolderName = "InGameTutorial";

	[SerializeField]
	private string tutorialKey = string.Empty;

	[SerializeField]
	private string startTutorialStepKey = "Start";

	[SerializeField]
	public InGameTutorialStep cStep;

	[SerializeField]
	public GameObject panelPrefab;

	[SerializeField]
	private static InGameTutorBlockPanel panel;

	public int depthPanel;

	public bool _fromStart;

	public string step;

	public bool blockTutor;

	private bool tutorialStarted;

	public override void Awake()
	{
		if (inited)
		{
			return;
		}
		InGameTutorial[] array = Object.FindObjectsOfType<InGameTutorial>();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i].tutorialKey == tutorialKey && array[i] != this)
			{
				Object.Destroy(base.gameObject);
			}
		}
		inited = true;
		Object.DontDestroyOnLoad(base.gameObject);
		base.Awake();
	}

	public string GetStepName()
	{
		if (cStep == null)
		{
			if (tutorialStarted)
			{
				return string.Empty;
			}
			return "NotStarted";
		}
		return cStep.name;
	}

	public override void Init()
	{
		base.Init();
		InitEvents(new string[1] { "TutorialEvent" });
	}

	public static void StartTutorial(string name, Transform parent, string startTutorialStep = "", bool forceStart = false)
	{
		InGameTutorial inGameTutorial = null;
		InGameTutorial[] array = Object.FindObjectsOfType<InGameTutorial>();
		foreach (InGameTutorial inGameTutorial2 in array)
		{
			if (inGameTutorial2.tutorialKey == name)
			{
				inGameTutorial = inGameTutorial2;
				break;
			}
		}
		if (inGameTutorial == null)
		{
			return;
		}
		if (inGameTutorial.blockTutor)
		{
			BCWStorage.Set(inGameTutorial.tutorialKey, "Ended");
		}
		inGameTutorial.tutorialStarted = true;
		if (panel == null)
		{
			panel = Object.Instantiate(inGameTutorial.panelPrefab).GetComponent<InGameTutorBlockPanel>();
		}
		panel.transform.parent = parent;
		panel.transform.position = Vector3.zero;
		panel.transform.localScale = Vector3.one;
		if (!startTutorialStep.Equals(string.Empty))
		{
			inGameTutorial.startTutorialStepKey = startTutorialStep;
		}
		if (inGameTutorial.cStep == null || inGameTutorial.cStep.reloadTutorOnStart || inGameTutorial._fromStart)
		{
			string empty = string.Empty;
			empty = BCWStorage.Get<string>(inGameTutorial.tutorialKey);
			if (inGameTutorial._fromStart)
			{
				if (string.IsNullOrEmpty(inGameTutorial.step))
				{
					BCWStorage.Set(inGameTutorial.tutorialKey, inGameTutorial.startTutorialStepKey);
				}
				else
				{
					BCWStorage.Set(inGameTutorial.tutorialKey, inGameTutorial.step);
				}
				inGameTutorial._fromStart = false;
			}
			empty = BCWStorage.Get<string>(inGameTutorial.tutorialKey);
			if (string.IsNullOrEmpty(empty))
			{
				empty = inGameTutorial.startTutorialStepKey;
			}
			if (empty != "Ended")
			{
				inGameTutorial.LoadStep(empty);
				return;
			}
			if (inGameTutorial.cStep != null)
			{
				Object.Destroy(inGameTutorial.cStep.gameObject);
			}
			Object.Destroy(inGameTutorial);
			panel.EnableBlock(false);
		}
		else
		{
			if (inGameTutorial.cStep.startStepMessage.Length > 0)
			{
				inGameTutorial.SetText(inGameTutorial.cStep.startStepMessage[0]);
			}
			inGameTutorial.cStep.UIroot = NGUITools.GetRoot(panel.gameObject);
			inGameTutorial.BlockUI(inGameTutorial.cStep.blockScreen);
			inGameTutorial.cStep.StartTutorialStep();
		}
	}

	public void LoadStep(string stepName)
	{
		if (cStep != null)
		{
			Object.Destroy(cStep.gameObject);
		}
		SetText(string.Empty);
		if (stepName == string.Empty)
		{
			BlockUI(false);
			BCWStorage.Set(tutorialKey, "Ended");
			return;
		}
		try
		{
			cStep = Object.Instantiate(Resources.Load<GameObject>(TutorialRootFolderName + "/" + tutorialKey + "/" + stepName)).GetComponent<InGameTutorialStep>();
		}
		catch
		{
			Object.Destroy(panel.gameObject);
			return;
		}
		cStep.transform.parent = base.transform;
		cStep.UIroot = NGUITools.GetRoot(panel.gameObject);
		if (!cStep.ChkOtherStepBlock())
		{
			BlockUI(cStep.blockScreen);
		}
		if (cStep.saveStep)
		{
			BCWStorage.Set(tutorialKey, stepName);
		}
		cStep.AddEventListener(this, "Started", delegate
		{
		});
		cStep.AddEventListener(this, "Ended", delegate(Object sender, object[] args)
		{
			if (!BCWStorage.Get<bool>(tutorialKey + "SaveStep" + args[0].ToString()))
			{
				BCWStorage.Set(tutorialKey + "SaveStep" + args[0].ToString(), true, true);
			}
		});
		cStep.AddEventListener(this, "PrintText", delegate(Object sender, object[] args)
		{
			SetText((string)args[0]);
		});
		cStep.AddEventListener(this, "StartNextStep", delegate(Object sender, object[] args)
		{
			string stepName2 = (string)args[0];
			LoadStep(stepName2);
		});
		cStep.AddEventListener(this, "ShowElement", delegate(Object sender, object[] args)
		{
			panel.UnblockUIElement((GameObject)args[0], (bool)args[1]);
		});
		cStep.StartTutorialStep();
	}

	public void SetText(string term)
	{
		if (term != string.Empty)
		{
			if (depthPanel != 0)
			{
				panel.Depth(depthPanel);
			}
			else
			{
				panel.Depth(110);
			}
		}
		panel.SetText(ScriptLocalization.Get("respect_tutor/" + term), term != string.Empty);
	}

	public void BlockUI(bool val)
	{
		panel.EnableBlock(val);
	}

	public static void UnblockUIElement(GameObject obj, bool val, bool onAllObject = true)
	{
		panel.UnblockUIElement(obj, val, onAllObject);
	}

	public void TutorialEvent(string name)
	{
		RiseEvent(this, "TutorialEvent", name);
	}
}
